﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Defaults;
using Blueprints.Defaults.Converters;
using Blueprints.Resolution;
using System;
using System.Collections.Generic;
using System.Xml;

namespace Blueprints
{
    partial class DefaultTypeResolver
    {
        private IEnumerable<KeyValuePair<Type, IValueDescriptor>> GetValueDescriptors()
        {
            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(string),
                new DelegateValueDescriptor<string>((value) => { return value; }, (value) => { return value; }));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(decimal), 
                new DelegateValueDescriptor<decimal>(XmlConvert.ToString, XmlConvert.ToDecimal, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Boolean), 
                new DelegateValueDescriptor<Boolean>(XmlConvert.ToString, XmlConvert.ToBoolean, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(SByte), 
                new DelegateValueDescriptor<SByte>(XmlConvert.ToString, XmlConvert.ToSByte, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Int16), 
                new DelegateValueDescriptor<Int16>(XmlConvert.ToString, XmlConvert.ToInt16, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Int32), 
                new DelegateValueDescriptor<Int32>(XmlConvert.ToString, XmlConvert.ToInt32, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Int64), 
                new DelegateValueDescriptor<Int64>(XmlConvert.ToString, XmlConvert.ToInt64, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Byte), 
                new DelegateValueDescriptor<Byte>((value) => { return XmlConvert.ToString((UInt32)value); }, XmlConvert.ToByte, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(UInt16), 
                new DelegateValueDescriptor<UInt16>((value) => { return XmlConvert.ToString((UInt32)value); }, XmlConvert.ToUInt16, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(UInt32), 
                new DelegateValueDescriptor<UInt32>(XmlConvert.ToString, XmlConvert.ToUInt32, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(UInt64), 
                new DelegateValueDescriptor<UInt64>(XmlConvert.ToString, XmlConvert.ToUInt64, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Char), 
                new DelegateValueDescriptor<Char>(XmlConvert.ToString, XmlConvert.ToChar, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Single), 
                new DelegateValueDescriptor<Single>(XmlConvert.ToString, XmlConvert.ToSingle, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Double), 
                new DelegateValueDescriptor<Double>(XmlConvert.ToString, XmlConvert.ToDouble, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Guid), 
                new DelegateValueDescriptor<Guid>(XmlConvert.ToString, XmlConvert.ToGuid, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(Uri), 
                new DelegateValueDescriptor<Uri>((uri) => { return uri.ToString(); }, (str) => { return new UriBuilder(str).Uri; }, true));

            yield return new KeyValuePair<Type, IValueDescriptor>(
                typeof(DateTime), 
                new DelegateValueDescriptor<DateTime>(
                (dateTime) =>
                {
                    return DateTimeParser.ToString(dateTime);
                },
                (str) =>
                {
                    DateTime dateTime;
                    if (!DateTimeParser.TryParse(str, out dateTime))
                    {
                        throw new InvalidCastException("'" + str + "' is not a valid Xml dateTime");
                    }

                    return dateTime;
                }, 
                true));
        }
    }
}